<template>
  <div class="home-container">
    <!-- 导航栏 -->
    <van-nav-bar title="标题" class="page-nav-bar" fixed>
      <van-button
        class="search-btn"
        slot="title"
        type="info"
        size="small"
        round
        icon="search"
        to="/search"
        >搜索</van-button
      >
    </van-nav-bar>
    <!-- 导航栏 频道列表-->
    <van-tabs v-model="active" animated swipeable border class="channel-tabs">
      <van-tab
        :title="channel.name"
        v-for="channel in channels"
        :key="channel.id"
      >
        <article-list :channel="channel"></article-list>
      </van-tab>

      <!-- 右侧自定义内容 -->
      <!-- 占位元素 -->
      <template>
        <div class="placeholder"></div>
      </template>
      <!-- 右侧按钮 汉堡按钮-->
      <template #nav-right>
        <div class="hamburger-btn" @click="isChannelEditShow = true">
          <i class="toutiao toutiao-gengduo"></i>
        </div>
      </template>
    </van-tabs>
    <!-- 频道编辑弹出层 -->
    <van-popup
      v-model="isChannelEditShow"
      closeable
      position="bottom"
      :style="{ height: '100%' }"
    >
      <!-- 把弹出层中的内容抽离到一个组件方便管理 -->
      <!-- 父传子 -->
      <channel-edit
        :my-channels="channels"
        :active="active"
        @updata-active="onUpdateActive"
      ></channel-edit>
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user.js'
import ArticleList from './components/article-list.vue'
import ChannelEdit from './components/channel-edit.vue'
import { mapState } from 'vuex'
import { getItem } from '@/utils/storage.js'

export default {
  name: 'home',
  components: { ArticleList, ChannelEdit },
  props: {},
  data() {
    return {
      active: 0,
      channels: [],
      isChannelEditShow: false, // 控制频道弹出层
    }
  },
  computed: { ...mapState(['user']) },
  watch: {},
  created() {
    // 判断是否登录
    // 已登录
    if (this.user) {
      this.loadChannels()
    } else {
      // 未登录
      const localChannels = getItem('TOUTIAO_CHANNELS')
      if (localChannels) {
        this.channels = localChannels
      } else {
        this.loadChannels()
      }
    }
  },
  mounted() {},
  methods: {
    // 获取频道列表的数据
    async loadChannels() {
      try {
        const { data } = await getUserChannels()
        // console.log(data)
        // 数据保存到变量
        this.channels = data.data.channels
      } catch (err) {
        console.log(err)
        this.$toast('获取频道数据失败')
      }
    },
    // 处理子组件触发的自定义事件
    onUpdateActive(index, isShow) {
      // console.log('--', index)
      // 1.切换tab栏
      this.active = index
      // 2.隐藏弹出层
      this.isChannelEditShow = isShow
    },
  },
}
</script>

<style lang="less" scoped>
.home-container {
  padding-top: 174px;
  padding-bottom: 100px;
  // tabs 标签导航也设置为固定定位
  /deep/ .van-tabs__wrap {
    position: fixed;
    top: 92px;
    z-index: 1;
    left: 0;
    right: 0;
    height: 82px;
  }
  /deep/ .van-nav-bar__title {
    max-width: unset;
  }
  .search-btn {
    width: 555px;
    height: 64px;
    background-color: #5babfb;
    border: none;
    font-size: 28px;
    .van-icon {
      font-size: 32px;
    }
  }
  /deep/ .channel-tabs {
    .van-tab {
      border-right: 1px solid #edeff3;
      min-width: 200px;
      font-size: 30px;
      color: #777777;
    }
    .van-tab--active {
      color: #333333;
    }
    .van-tabs__nav {
      padding-bottom: 0;
    }
    .van-tabs__line {
      bottom: 8px;
      width: 31px !important;
      height: 6px;
      background-color: #3296fa;
    }
    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }
    .hamburger-btn {
      position: fixed;
      right: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      width: 66px;
      height: 82px;
      background-color: #fff;
      background-color: rgba(255, 255, 255, 0.902);
      i.toutiao {
        font-size: 33px;
      }
      &:before {
        content: '';
        position: absolute;
        left: 0;
        width: 1px;
        height: 58px;
        background-image: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
